<?php

namespace app\common\model;

use think\Collection;
use think\db\Query;

/**
 * Class Area
 * @package app\model
 * @property int $id
 * @property string $name
 * @property string $nid
 * @property int $status
 * @property int $pid
 * @property int $province
 * @property int $city
 * @property string $domain
 * @property int $order
 * @property int $areas_num
 * @property string $value
 * @property string $label
 * @property Collection $children
 * @method $this cascader()
 * @method $this shortArea() 简短的城市
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class Area extends \app\common\model\BaseModel
{
    protected $name = 'Area';

    public function parent()
    {
        return $this->belongsTo(Area::class, 'pid', 'id');
    }

    public function children()
    {
        return $this->hasMany(Area::class, 'pid', 'id');
    }

    public function getValueAttr($value, $data)
    {
        return $data['id'];
    }

    public function getLabelAttr($value, $data)
    {
        return $data['name'];
    }

    // 给 element-plus 的级联选择器用的
    public function scopeCascader(Query $query)
    {
        $query->field(['id', 'pid', 'name'])
            ->append(['value', 'label'])
            ->hidden(['id', 'pid', 'name'])
            ->order('areas_num', 'asc');
    }

    public function scopeShortArea(Query $query)
    {
        $query->field(['id', 'name']);
    }

    /**
     * 1对1
     * @return \think\model\relation\HasOne
     */
    //    public function one(){
    //        return $this->hasOne( Test1::class, 'Test1表中的键', 'Area表中的键');
    //    }

    /**
     * 1对多
     * @return \think\model\relation\HasMany
     */
    //    public function many(){
    //        return $this->hasMany( Test1::class, 'Test1表中的键', 'Area表中的键');
    //    }

    /**
     * 多对多
     * @return \think\model\relation\BelongsToMany
     */
    //    public function belongs(){
    //        return $this->belongsToMany( Test1::class, '中间表', '中间表中Test1的键', '中间表中Area的键');
    //    }
}
